<?php
	session_start();

	include_once("./database/dbutil.php");

	$succesful = true;
	if(isset($_POST['username']) && isset($_POST['password'])){
		$username = $_POST['username'];
		$password = $_POST['password'];
		
		$dbUtil = new DBUtil();
		$succesful = $dbUtil -> checkCredentials($username, $password);
		
		if($succesful) {
			$_SESSION['username'] = $username;
			$_SESSION['loggedIn'] = true;
			
			header("Location: ./index.php");
		}
	}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Lightcontrol</title>

    <!-- Bootstrap libraries -->
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    
	<!-- Custom styles for this template -->
   
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
  	<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
      	<div class="navbar-header">
       	  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Lightcontrol</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li><a id="index" href="index.php">Home</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Electronics<b class="caret"></b></a>
			  <ul class="dropdown-menu">
				<li><a id="buildings" href="buildings.php">Geb&auml;ude</a></li>
				<li><a id="rooms" href="rooms.php">R&auml;ume</a></li>
				<li><a id="plugs" href="plugs.php">Steckdosen</a></li>
			  </ul>
			</li>
            <li><a id="contact" href="contact.php">Kontakt</a></li>
          </ul>
		  <ul class="nav navbar-nav navbar-right">
			<li class="active"><a id="login" href="login.php">Login</a></li>
		  </ul>
        </div><!--/.nav-collapse -->
      </div>
    </div>
    <br />
	<div class="container">    
	  <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
	    <div class="panel panel-info" >
	      <div class="panel-heading">
	      	<div class="panel-title">Log In</div>
	      </div>     
	      <div style="padding-top:30px" class="panel-body">
          	<div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
		    <form id="loginform" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" class="form-horizontal" role="form">
              <div style="margin-bottom: 25px" class="input-group">
                <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                <input id="login-username" type="text" class="form-control" name="username" value="" placeholder="Personalnummer">                                        
              </div>
              <div style="margin-bottom: 25px" class="input-group">
                <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                <input id="login-password" type="password" class="form-control" name="password" placeholder="Passwort">
              </div>
              <div style="margin-top:10px" class="form-group">
                <!-- Button -->
                <div class="col-sm-12 controls">
                  <a id="btn-login" href="#" onclick="$(this).closest('form').submit()" class="btn btn-success">Login</a>
				  <label><?php if(!$succesful) { echo "Login fehlgeschlagen";} ?></label>
                </div>
              </div>
            </form>     
          </div>                     
	    </div>  
	  </div>
    </div>
  </body>
</html>